package com.leanyuan.controller;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.leanyuan.domain.StaffMsg;
import com.leanyuan.publi.domain.HousDept;
import com.leanyuan.publi.service.HousDeptService;
import com.leanyuan.service.StaffMsgService_V;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author 相亲相爱一家人
 * @since 2021-05-25
 */
@RestController
@RequestMapping("/leanyuan/hous-dept")
public class HousDeptController {
    @Autowired
    HousDeptService housDeptService;

    @Autowired
    StaffMsgService_V staffMsgServiceV;

    //查询部门所有信息并分页
    @RequestMapping("findDeptPage")
    public IPage<HousDept> findDeptPage(Integer page, Integer pageSize, Model model){
        Page<HousDept> iPage=new Page<HousDept>(page,pageSize);
        IPage<HousDept> housDeptIPage= this.housDeptService.page(iPage,null);
        model.addAttribute("housDeptIPage",housDeptIPage);
        return housDeptIPage;
    }

    //带条件的查询部门所有信息并分页
    @RequestMapping("findDeptPageByParam")
    public IPage<HousDept> findDeptPageByParam(Integer deptId,String deptName,Integer page, Integer pageSize, Model model){
        QueryWrapper<HousDept> queryWrapper=new QueryWrapper<HousDept>();
        if(deptId !=null && deptId >=0) {
            queryWrapper.eq("p_id", deptId);
        }else if(deptName != null && deptName != ""){
            queryWrapper.eq("p_name",deptName);
        }
        Page<HousDept> iPage=new Page<HousDept>(page,pageSize);
        IPage<HousDept> housDeptIPage= this.housDeptService.page(iPage,queryWrapper);
        model.addAttribute("housDeptIPage",housDeptIPage);
        return housDeptIPage;
    }

    //添加部门信息
    @RequestMapping("addHousDept")
    public boolean addHousDept(String deptname){
        HousDept housDept=new HousDept();
        housDept.setPName(deptname);
        return this.housDeptService.save(housDept);
    }

    //修改部门信息
    @RequestMapping("updateHousDept")
    public boolean updateHousDept(@RequestBody HousDept housDept){
        return this.housDeptService.updateById(housDept);
    }

    //根据Id删除部门信息
    @RequestMapping("deleteHousDept")
    public boolean deleteHousDept(Integer id){
        QueryWrapper<StaffMsg> queryWrapper = new QueryWrapper<StaffMsg>();
        queryWrapper.eq("p_id",id);

        List<StaffMsg> list=this.staffMsgServiceV.list(queryWrapper);

        if(list.size()<=0) {
            return this.housDeptService.removeById(id);
        }
        return false;
    }
}

